package action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import service.UsersService;
import util.DateUtil;
import util.MapUtil;
import util.ActionUtil;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import entity.Users;
public class UserAction extends ActionSupport implements ModelDriven<Users>   {
	private UsersService userService=new UsersService();
	private Users user=new Users();
    private Map<String,Object> loginSuccess= new HashMap<String, Object>();
    private Map<String,Object> addUserSuccess= new HashMap<String, Object>();
    private Map<String,Object> addUserFailed= new HashMap<String, Object>();
    private Map<String,Object> listUserSuccess= new HashMap<String, Object>();
    private Map<String,Object> deleteUserSuccess= new HashMap<String, Object>();
    private Map<String,Object> updateUserSuccess= new HashMap<String, Object>();
	
	public String login() throws Exception {

	   String flag="";
       loginSuccess.clear();
		List<String> username_list = userService.check(user);
		if(!username_list.isEmpty()){
			loginSuccess.put("user",user);
			loginSuccess.put("status", "1002");
			loginSuccess.put("username",username_list.get(0));
			flag="success";
		}else{
		   flag="failed";
		   MapUtil.setStatus(loginSuccess, "1001");
		   MapUtil.setOther(loginSuccess, "登录失败");
		}
		return flag;
	}
	//添加用户测试不了birthday设置为了日期格式无法直接自己给参数
	//一直没注入到user中去
	public String addUsers(){
		String flag="";
		ActionUtil.kuayu();
		addUserSuccess.clear();
		addUserFailed.clear();
		if(userService.addUser(user)){
		   MapUtil.setStatus(addUserSuccess, "1002");
		   MapUtil.setOther(addUserSuccess, "添加成功");
		   flag="success";
		}else{
	      MapUtil.setStatus(addUserFailed, "1001");
	    	  MapUtil.setOther(addUserFailed, "用户已存在");
		  flag="false";
		}
		  return flag;
	}
	//显示全部用户，没设置失败的情况
	public String  listUser(){
		ActionUtil.kuayu();
		List<Users> list=userService.listUser();
		for(int i = 0; i < list.size(); i++ ){
			String birth = DateUtil.parseToString(list.get(i).getBirthday(), DateUtil.yyyyMMdd);
			list.get(i).setStrBirth(birth);
		}
		listUserSuccess.clear();
		MapUtil.setStatus(listUserSuccess, "1002");
		MapUtil.setOther(listUserSuccess, list);
		MapUtil.setTotalNumber(listUserSuccess, list.size());
		return  "success";
	}


	//删除用户信息
	public String deleteUser(){
		ActionUtil.kuayu();
		deleteUserSuccess.clear();
		Long idd = user.getId();
		Users userForDel = userService.searchUserById(idd);
		userService.deleteUser(userForDel);
		MapUtil.setStatus(deleteUserSuccess, "1002");
		MapUtil.setOther(deleteUserSuccess, "删除用户成功");////////////
		return SUCCESS;
		
	}
	//更新用户信息
	public  String updateUser(){
		ActionUtil.kuayu();
		updateUserSuccess.clear();
		userService.updateUser(user);
		MapUtil.setStatus(updateUserSuccess, "1002");
		MapUtil.setOther(updateUserSuccess, "更新用户成功");//////////
		return SUCCESS;
		
	}
	
	public Map<String, Object> getLoginSuccess() {
		return loginSuccess;
	}
	public Map<String, Object> getAddUserSuccess() {
		return addUserSuccess;
	}
	public Map<String, Object> getAddUserFailed() {
		return addUserFailed;
	}
	public Map<String, Object> getListUserSuccess() {
		return listUserSuccess;
	}
	public Map<String, Object> getDeleteUserSuccess() {
		return deleteUserSuccess;
	}
	public Map<String, Object> getUpdateUserSuccess() {
		return updateUserSuccess;
	}
	@Override
	public Users getModel() {
		// TODO 自动生成的方法存根
		return user;
	}
}
